package com.jxzn.locust.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    PermissionInterceptorHandler permissionInterceptorHandler;

    @Value("${upload.home.path}")
    String uploadFolder;

    @Value("${upload.home.url}")
    String fileUrl;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptorHandler()).addPathPatterns("/bg/**")
                .excludePathPatterns("/static/**");
        registry.addInterceptor(permissionInterceptorHandler).addPathPatterns("/bg/**");
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(fileUrl + "**").addResourceLocations("file:" + uploadFolder);
        registry.addResourceHandler("/pk/**").addResourceLocations("file:" + uploadFolder + "pk/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

}
